package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.form.CartRequest;
import com.cskaoyan.shopping.form.CheckAllItemRequest;
import com.cskaoyan.shopping.service.ICartService;
import org.redisson.Redisson;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author wxgl
 * @date 2023-01-10 15:21
 * @describe
 */
@Service
public class ICartServiceImpl implements ICartService {
    @Override
    public List<CartProductDto> getCartListById(Long userId) {
        return null;
    }

    @Override
    public void addToCart(CartRequest request) {

    }

    @Override
    public void updateCartNum(CartRequest request) {

    }

    @Override
    public void checkAllCartItem(CheckAllItemRequest request) {

    }

    @Override
    public void deleteCartItem(Long uid, Long itemId) {

    }

    @Override
    public void deleteCheckedItem(Long userId) {

    }

    @Override
    public void deleteCarts(String uid,String pid) {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");

        RedissonClient redissonClient = Redisson.create(config);
        //获取hash的map（其实就相当于一张表格）
        //其中uid是表名
        //pid是key
        //value是对应的商品id的购物信息
        RMap<Object, Object> map = redissonClient.getMap(uid);
        if (map == null){
            throw new BizException(SysRetCodeConstants.DATA_NOT_EXIST.getMessage());
        }
        map.remove(pid);
    }

    @Override
    public void deleteCartsSelected(String uid) {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");

        RedissonClient redissonClient = Redisson.create(config);
        RMap<String, CartProductDto> map = redissonClient.getMap(uid);
        Collection<CartProductDto> CartProductDtos = map.values();
        for (CartProductDto cartProductDto : CartProductDtos) {
            String checked = cartProductDto.getChecked();
            if (checked.equals("true")){
                Long productId = cartProductDto.getProductId();
                map.remove(productId.toString());
            }
        }
    }

    @Override
    public void selectAllCartOrNot(Map map) {
        String userId = (String) map.get("userId");
        String checked = (String) map.get("checked");

        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");

        RedissonClient redissonClient = Redisson.create(config);
        RMap<String, CartProductDto> map1 = redissonClient.getMap(userId);
        Collection<CartProductDto> cartProductDtos = map1.values();

        //遍历所有的value，把所有的cartProductDto里面的checked改为参数里面的checked
        //然后把map1里面的对应的pid的数据删掉
        //把修改后的数据插入
        for (CartProductDto cartProductDto : cartProductDtos) {
            cartProductDto.setChecked(checked);
            Long productId = cartProductDto.getProductId();
            map1.remove(productId.toString());
            map1.put(productId.toString(),cartProductDto);
        }
    }
}
